﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InfoStuffStore.Common;

namespace InfoStuffStore
{
    public partial class FormProduct : System.Web.UI.Page
    {
        private int? ratecount = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetProductReviews();
                GetProductDetails();
                GetProductSpecifications();
            }
        }

        private void GetProductDetails()
        {
            int productId = Convert.ToInt32(Request.QueryString["pid"]);
            using (DBDataContext dataContext = new DBDataContext())
            {
                var result = dataContext.GetProductDetails(productId);
                foreach (var item in result)
                {
                    lblSKU.Text = item.products_sku;
                    lblMPN.Text = item.products_mpn;
                    lblGTIN.Text = item.products_gtin;
                    lblManufacture.Text = item.BrandName;
                    lblModel.Text = item.products_model;
                    lblPrice.Text = item.products_price.Value.ToString();
                    lblLongDesc.InnerHtml = item.products_long_description;
                    if (ratecount.Value != 0)
                    {
                        raterTotal.Value = item.TotalRating / ratecount.Value;
                    }
                    else
                    {
                        raterTotal.Value = 0;
                    }
                    string searchKey = string.Empty;
                    if (!string.IsNullOrWhiteSpace(item.products_mpn))
                    {
                        searchKey += item.products_mpn;
                    }
                    if (!string.IsNullOrWhiteSpace(item.products_sku))
                    {
                        searchKey += "," + item.products_sku;
                    }
                    
                    if (!string.IsNullOrWhiteSpace(item.products_gtin))
                    {
                        searchKey += "," + item.products_gtin;
                    }

                    GetProductAlternates(item.brands_id.Value);
                    string script = "<dl class=\"box boxTabsInfo box01\" style=\"width: 95%;\"><dd><script id=\"reviewScript\" runat=\"server\" type=\"text/javascript\" language=\"javascript\" src=\"http://partner.testseek.com/export/search.php?ts_key=1D384607&ts_ver=3.5&ts_type=4&ts_limit=10&ts_mpn={0}\"></script></dd></dl>";
                    divReview.InnerHtml = string.Format(script, searchKey);
                }
            }

        }

        public List<string> ProdImages
        {
            get
            {
                int productId = Convert.ToInt32(Request.QueryString["pid"]);
                List<string> imglist = new List<string>();
                using (DBDataContext dataContext = new DBDataContext())
                {
                    var result = dataContext.GetProductImages(productId);
                    foreach (var item in result)
                    {
                        imglist.Add(item.external_url);
                    }
                }

                return imglist;
            }
        }

        private void GetProductSpecifications()
        {
            int productId = Convert.ToInt32(Request.QueryString["pid"]);
            using (DBDataContext dataContext = new DBDataContext())
            {
                GridProductSpec.DataSource = dataContext.GetProductSpecifications(productId);
                GridProductSpec.DataBind();
            }
        }

        private void GetProductReviews()
        {
            int productId = Convert.ToInt32(Request.QueryString["pid"]);
            using (DBDataContext dataContext = new DBDataContext())
            {
                var result = dataContext.GetProductReview(productId, ref ratecount);
                GridProductReviews.DataSource = result;
                GridProductReviews.DataBind();
            }
        }

        private void GetProductAlternates(int brandId)
        {
            using (DBDataContext dataContext = new DBDataContext())
            {
                var result = dataContext.GetAlternateProducts(brandId);
                GridProductAlternates.DataSource = result;
                GridProductAlternates.DataBind();
            }
        }

        protected void btnReview_Click(object sender, EventArgs e)
        {
            int productId = Convert.ToInt32(Request.QueryString["pid"]);
            if (!string.IsNullOrWhiteSpace(txtName.Text) && !string.IsNullOrWhiteSpace(txtReviews.Text))
            {
                using (DBDataContext dataContext = new DBDataContext())
                {
                    dataContext.AddReviews(productId, txtName.Text, txtReviews.Text, raterUser.Value);
                }

                GetProductReviews();
                txtName.Text = "";
                txtReviews.Text = "";
                raterUser.Value = 0;
            }
            else
            {
                ScriptManager.RegisterStartupScript(pnlUpdate, this.GetType(), "alert", "alert('All fields are required!')", true);
            }
        }
    }
}